package com.ketai.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.ketai.dao.StyleDao;
import com.ketai.dao.impl.StyleDaoImpl;
import com.ketai.service.StyleService;
import com.ketai.util.Pages;
import com.ketai.vo.ApplicationVo;
import com.ketai.vo.StyleVo;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:长沙科泰梦想学院-蓝色小药丸
 * @Mission:打造演说·创客型跨境电商与互联网人才
 * @Description:
 * @Date:Create In 2022/8/10 23:19
 **/
public class StyleServiceImpl implements StyleService {

    StyleDao dao = new StyleDaoImpl();

    @Override
    public int saveStyle (String id, String paths, String uid) {
      paths= paths.substring(1, paths.length()-1);
        System.out.println("====:  "+paths);

        Object[] param = {id, paths, uid};
        try {
            return dao.saveImageUrls(param);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }


        return 0;
    }

    @Override
    public Pages<StyleVo> getStyleList (String title,String checked,String  curr,String  limit) {

        Pages<StyleVo>  result = new Pages<StyleVo>();

     StringBuilder  sb=new StringBuilder("select b.* ,u.`name` checkedName  from (select  s.id,s.paths,s.createTime ,t.name typeName,s.checked ,s.checkedid, s.checkedTime ,o.`name` oName,c.title, u1.nickName from  style s ,organizationbase o ,c_activity c  ,User u1 ,activity_type t where  s.a_id=c.id ");
     sb.append(" and s.uid= u1.id  and c.pbid=o.id and c.tid=t.id) b  left join  background_user u on u.id= checkedId ");

        String  sql2=" select count(1) total from (select  s.id,s.paths,s.createTime ,s.checked ,s.checkedid, s.checkedTime ,o.`name` oName,c.title, u1.nickName from  style s ,organizationbase o ,c_activity c  ,User u1  ,activity_type t where  s.a_id=c.id "
                +"    and s.uid= u1.id  and c.pbid=o.id and c.id=t.id) b  left join  background_user u on u.id= checkedId ";
        List params = new ArrayList<>();
        if (!StrUtil.isEmpty(title)) {
            sb.append("  and title like concat('%',?,'%')");
            sql2 += "  and title like concat('%',?,'%')";
            params.add(title);
        }

        if (!StrUtil.hasEmpty(checked)) {
            sb.append(" and achecked=? ");
            sql2 +="   and achecked=? ";
            params.add(checked);
        }

        try {

            long total=  dao.getStyleCount(sql2, params.toArray());
            result.setDataTotal(total);
            int pageSize=limit==null?5: Convert.toInt(limit);
            int pagenum=curr==null?1: Convert.toInt(curr);
            result.setPageNumber(pageSize);
            int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
            result.setTotalPages(pages);
            result.setCurrentPage(pagenum);
            sb.append("  order by createTime desc   limit ?,? ");

            params.add((pagenum - 1) * pageSize);
            params.add( pageSize);

            List<StyleVo> list = dao.getStyleList(sb.toString(), params.toArray());
            result.setDataItems(list);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int checkedActity (String id, String open, String desc, String checkedId) {

        String sql="update style set checked=? ";
        List params = new ArrayList();
        if (!StrUtil.isBlank(open)&&open.equals("on")) {
            params.add(1);
        }else {
            sql+="  ";
            params.add(2);

        }
        sql += " , checkedTime=now() ,checkedId=? where id=?";
        params.add(checkedId);
        params.add(id);
        System.out.println(sql);
        try {
            return  dao.checkedActity(sql, params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
